<?php
namespace app\common\controller;

use app\common\controller\ApiResponse;

class ApiAction extends ApiResponse
{
    /**
     * @Title: 构造方法
     * @Description: todo(判断是否登陆)
     * @param string token 
     * @return json
     * @author 若谷
     * @date 2019年5月22日
     * @throws
     */
    public function __construct(){
        parent::__construct();
        $token = isset($_REQUEST["token"])?trim($_REQUEST["token"]):'';
        if(empty($token)){
            echo $this->response(lang('fail_code'),'未登录');exit();
        }
        $model = new \app\common\model\Member;
        $member = $model->where("token",$token)->find();
        if (!$member) {
            echo $this->response(lang('fail_code'),'登陆失效');exit();
        }else{
            define('MID', $member['id']);   //设置登陆用户ID常量
            define('OPENID', $member['openid']);
        }
    }

    /**
     * @Title: create
     * @Description: todo(基础单表新增)
     * @return mixed
     * @author 
     * @date 2018年1月15日
     * @throws
     */
    protected function create($data,$modelName,$model)
    {
        $result = $this->validate($data, 'app\common\validate\\'.$modelName.'.add');
        if(true !== $result){
            return ['code'=>0,'msg'=>$result];
        }
        $result = $model->allowField(true)->save($data);
        if ($result){
            return ['code'=>1,'msg'=>'成功'];
        }else{
            return ['code'=>0,'msg'=> $model->getError()];
        }
    }

    /**
     * @Title: discount
     * @Description: todo(折扣金额)
     * @param couponLogsId 领取优惠卷表id
     * @return mixed
     * @author 
     * @date 2019年6月3日
     * @throws
     */
    protected function discount($price,$couponLogsId,$uid=0){
        $discount = 0;
        $CouponLogsModel = new \app\common\model\CouponLogs;
        $platform = $CouponLogsModel->where("id",$couponLogsId)->where("is_use",0)->where("mid",MID)->find();
        $platformInfo = $platform->useCoupons;
        if ($platformInfo) {
            if ($platformInfo->uid == $uid) {
                $min_amount = $platformInfo->min_amount;
                if ($price>=$min_amount) {
                    $discount = $platformInfo->money;
                }
            }
        }
        return $discount;
    }
}